/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef CAST_ENGINE_DLNA_COMMON_EVENT_INCLUDE_EVENT_CONTROLLER_H
#define CAST_ENGINE_DLNA_COMMON_EVENT_INCLUDE_EVENT_CONTROLLER_H

#include "common_event_subscriber.h"
#include "refbase.h"
#include "system_ability_status_change_stub.h"

#include "single_instance.h"
#include <memory>

namespace OHOS {
namespace CastEngine {
class CastEngineDlnaEventController : public EventFwk::CommonEventSubscriber, public std::enable_shared_from_this<CastEngineDlnaEventController> {
    DECLARE_SMART_PTR_SINGLE_INSTANCE_BASE(CastEngineDlnaEventController);

public:
    CastEngineDlnaEventController(const EventFwk::CommonEventSubscribeInfo &subscriberInfo)
        : CommonEventSubscriber(subscriberInfo) {}
    CastEngineDlnaEventController() {}
    ~ CastEngineDlnaEventController() {}

    virtual void OnReceiveEvent(const EventFwk::CommonEventData &data);
    void Init();
    void Stop();

    void RegisterOnConnectvityChange(std::function<void (int32_t)> func);

private:
    class SystemAbilityStatusChangeListener : public OHOS::SystemAbilityStatusChangeStub {
    public:
        SystemAbilityStatusChangeListener(std::weak_ptr<CastEngineDlnaEventController> defaultEventController);
        virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
        virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
        void Stop();

    private:
        std::weak_ptr<CastEngineDlnaEventController> subscriber_;
    };

    std::function<void (int32_t)> onConnectvityChange_;
    sptr<SystemAbilityStatusChangeListener> sysAbilityListener_ = nullptr;
};

} // namespace CastEngine
} // namespace OHOS

#endif  // CAST_ENGINE_DLNA_COMMON_EVENT_INCLUDE_EVENT_CONTROLLER_H